package com.xxxx.server;

import com.xxxx.server.Utils.DateUtils;
import com.xxxx.server.pojo.Admin;
import com.xxxx.server.pojo.RespBean;
import com.xxxx.server.pojo.Sleave;
import com.xxxx.server.service.ISleaveService;
import io.swagger.annotations.ApiOperation;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Calendar;
import java.util.Date;


@SpringBootTest
public class test {

    @Test
    public static void main1(String[] args) throws ParseException {
        SimpleDateFormat ft = new SimpleDateFormat ("MM-dd hh:mm:ss");
        Date startTime = ft.parse("07-00 18:00:00");
        Date endTime = ft.parse("7-11 23:00:00");
        String time = LocalDateTime.now().toString();
        time = time.substring(5,10) + " " + time.substring(11,19);
        Date nowtime = ft.parse(time);
        Calendar rightNow = Calendar.getInstance();
        rightNow.setTime(nowtime);
        rightNow.add(Calendar.MONTH,1);
        nowtime = rightNow.getTime();

        boolean effectiveDate = DateUtils.isEffectiveDate(nowtime, startTime, endTime);
        if (effectiveDate) {
            System.out.println("当前时间在范围内");
        }else {
            System.out.println("当前时间在不在范围内");
        }

    }

    /****
     * 传入具体日期 ，返回具体日期增加一个月。
     * @param date 日期(2017-04-13)
     * @return 2017-05-13
     * @throws ParseException
     */
    public static String subMonth(String date) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("MM-dd hh:mm:ss");
        Date dt = sdf.parse(date);
        Calendar rightNow = Calendar.getInstance();
        rightNow.setTime(dt);
        rightNow.add(Calendar.MONTH, 1);
        Date dt1 = rightNow.getTime();
        System.out.println(dt1);

        String reStr = sdf.format(dt1);
        return reStr;
    }

    @Test
    public static void main2(String[] args) throws ParseException {
        SimpleDateFormat ft = new SimpleDateFormat("MM-dd hh:mm:ss");
        String nowtime = LocalDateTime.now().toString();

        String startMonth = nowtime.substring(5,7);
        int end = Integer.parseInt (startMonth)+1;

        String endMonth = String.valueOf(end);
        Date startTime = ft.parse(startMonth+"-01 00:00:00");
        Date endTime = ft.parse(endMonth+"-01 00:00:00");

        nowtime = nowtime.substring(5,10) + " " + nowtime.substring(11,19);
        Date nowTime = ft.parse(nowtime);
//        System.out.println(startTime);
//        System.out.println(endTime);
//        System.out.println(nowTime);

        long time = System.currentTimeMillis();
        System.out.println(time);
        System.out.println(ft.format(time));
        System.out.println(ft.format(time));
        System.out.println(ft.format(time));
        System.out.println(ft.format(time));


        System.out.println("---------------");
        time += 60*60;
        System.out.println(time);
        System.out.println(ft.format(time));
        System.out.println(ft.format(time));
        System.out.println(ft.format(time));
        System.out.println(ft.format(time));
    }

    @Test
    public static void main(String[] args) throws ParseException{
        SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        String recordtime = "2022-06-30 00:00:00";
        Date rt = ft.parse(recordtime);
        Calendar rightNow = Calendar.getInstance();
        rightNow.setTime(rt);
        rightNow.add(Calendar.DAY_OF_MONTH, 7);
        Date endtime = rightNow.getTime();

        System.out.println(endtime);
        System.out.println(endtime);
        System.out.println(endtime);
        System.out.println(endtime);
        String nowtime = LocalDateTime.now().toString();
        nowtime = nowtime.substring(0,10) + " " + nowtime.substring(11,19);
        Date nowTime = ft.parse(nowtime);
        System.out.println(nowTime);
        System.out.println(nowTime);
        System.out.println(nowTime);
        System.out.println(nowTime);

    }

    @Autowired
    private ISleaveService sleaveService;

    @Test
    @ApiOperation(value = "添加学生请假信息")
    @PostMapping("/add")
    public RespBean addSleave(@RequestBody Sleave sleave){
        sleave.setCreatTime(LocalDateTime.now());
        sleave.setSLid(((Admin) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getId());
        if (sleaveService.save(sleave)){
            System.out.println(sleave.getId());

            return RespBean.success("添加成功！");
        }
        return RespBean.error("添加失败!");
    }

    @Test
    public void hhh()throws ParseException{
        SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        String recordtime = "2022-06-28 00:00:00";
        Date rt = ft.parse(recordtime);
        System.out.println(isNow(rt));
    }

    public static boolean isNow(Date date) {
        // 默认的年月日的格式. yyyy-MM-dd
        String PATTEN_DEFAULT_YMD = "yyyy-MM-dd";
        // 当前时间
        Date now = new Date();
        SimpleDateFormat sf = new SimpleDateFormat(PATTEN_DEFAULT_YMD);
        //获取今天的日期
        String nowDay = sf.format(now);
        //对比的时间
        String day = sf.format(date);

        return day.equals(nowDay);
    }

}
